﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using PortalCurriculo.WebUI.Helpers;

namespace PortalCurriculo.WebUI.UserControls.Basico
{
    public partial class Data : UserControlHelper
    {
        public DateTime? Value
        {
            get { return string.IsNullOrEmpty(Date.Text) ? (DateTime?)null : DateTime.Parse(Date.Text, CultureHelper.Cultura.DateTimeFormat) ; }
            set { Date.Text = value == null ? string.Empty : value.Value.ToShortDateString().ToString(CultureHelper.Cultura.DateTimeFormat) ; }
        }

        public string LabelText
        {
            get { return Label.Text; }
            set
            {
                Label.Visible = true;
                Label.Text = value;
            }
        }

        public string CssClass
        {
            get { return Date.CssClass; }
            set { Date.CssClass = value; }
        }
        public string Name { get; set; }
        
        public bool Obrigatorio
        {
            get { return RequiredFieldValidator.Visible; }
            set { RequiredFieldValidator.Visible = value; }
        }

        public Unit Width { private get; set; }
        public DateTime? MinimumValue { private get; set; }
        public DateTime? MaximumValue { private get; set; }

        public string ValidationGroup
        {
            get { return Date.ValidationGroup; }
            set { Date.ValidationGroup = RequiredFieldValidator.ValidationGroup = RangeValidator.ValidationGroup = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Date.ID += Name;
            CalendarExtender.TargetControlID = Date.ID;
            RangeValidator.ControlToValidate = Date.ID;
            RequiredFieldValidator.ControlToValidate = Date.ID;
        
            RangeValidator.MinimumValue = MinimumValue == null ? DateTime.Today.AddYears(-100).ToShortDateString() : MinimumValue.Value.ToShortDateString();
            RangeValidator.MaximumValue = MaximumValue == null ? DateTime.Today.AddYears(70).ToShortDateString() : MaximumValue.Value.ToShortDateString();
            CalendarExtender.Format = CultureHelper.Cultura.DateTimeFormat.ShortDatePattern;
    
            if (Width != 0)
                Date.Width = Width;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            Label.AssociatedControlID = Date.ID;
            base.Render(writer);
        }
    }
}